import {request, readBooks} from '../network/request'
import Vue from 'vue'
import { type } from 'jquery';

export default {
  //获取首页介绍分类信息
  asynGetHomeListData(context) {
    request({
      url: '/getCover.php',
      params: {
        table: 'homecontent'
      }
    }).then(res => {
      context.commit('getHomeListData', res);
    }).catch(err => {
      console.log(err);
    })
  },
  // 获取用户在线阅读过的书籍
  asynGetUserOnlineHistry(context, payload) {
    request({
      url: '/getUserOnlineHistry.php',
      params: {
        userId: payload
      }
    }).then(res => {
      context.commit('getUserOnlineHistry', res);
    }).catch(err => {
      console.log(err);
    })
  },
  // 请求轮播图数据
  asynGetDetailsTopBannerData(context) {
    request({
      url: '/getCover.php',
      params: {
        table: 'detailstopbanner'
      }
    }).then(res => {
      context.commit('getDetailsNavigation', res);
    }).catch(err => {
      console.log(err);
    })
  },
  // 请求分页后的书籍信息
  asynGetMobiBooks(context, payload) {
    request({
      url: '/getLimitList.php',
      params: payload
    }).then(res => {
      console.log(res);
      context.commit('getMobiBooks', res);
    }).catch(err => {
      console.log(err);
    })
  },
  // 请求书籍的总数
  asynGetMobiBooksTotal(context, payload) {
    request({
      url: '/getMobiBooksTotal.php',
      params: {
        title: payload
      }
    }).then(res => {
      context.commit('getMobiBooksTotal', res);
    }).catch(err => {
      console.log(err);
    })
  },
  // 通过Id获取当前书籍
  asynGetMobiBooksList(context) {
    request({
      url: '/hasIdGetBook.php',
      params: {
        bookTable: 'mobibooks',
        bookId: Number(localStorage.getItem('booksId'))
      }
    }).then(res => {
      context.commit('getMobiBooksList', res);
    }).catch(err => {
      console.log(err);
    })
  },
  // 通过Id写入用户在线阅读信息，并且请求对应的书签
  asynOnlineReadRecord(context) {
    request({
      url: '/getOnlineReadRecord.php',
      params: {
        userId: Number(JSON.parse(localStorage.getItem('userInformation')).id),
        bookId: Number(localStorage.getItem('booksId'))
      }
    }).then(res => {
      localStorage.setItem('nowScroll', res);
    }).catch(err => {
      console.log(err);
    })
  },
  // 保存用户阅读的书签
  asynStorageBookMark(context, payload) {
    request({
      url: '/storageBookMark.php',
      params: {
        userId: Number(JSON.parse(localStorage.getItem('userInformation')).id),
        bookId: Number(localStorage.getItem('booksId')),
        scroll: payload
      }
    }).then(res => {
      // console.log(res);
    }).catch(err => {
      console.log(err);
    })
  },
  // 获取用户评论
  asynGetUserComment(context) {
    request({
      url: '/getUserComment.php',
      params: {
        bookId: Number(localStorage.getItem('booksId'))
      }
    }).then(res => {
      context.commit('getUserComment', res);
    }).catch(err => {
      console.log(err);
    })
  },
  // 向后台发送用户评论，获取状态字符
  asynSetUserComment(context, payload) {
    request({
      url: '/setUserComment.php',
      params: payload
    }).then(res => {
      if (res == 'success') {
        window.location.reload();
      }
    }).catch(err => {
      console.log(err);
    })
  },
  // 用户登录
  asynGetUserL(context, payload) {
    request({
      url: '/getUserL.php',
      params: payload
    }).then(res => {
      context.commit('getUserData', res);
    }).catch(err => {
      console.log(err);
    })
  },
  // 用户注册
  asynGetUserR(context, payload) {
    request({
      url: '/getUserR.php',
      params: payload
    }).then(res => {
      context.commit('upLoadUserData', res);
    }).catch(err => {
      console.log(err);
    })
  },
  // 提交用户修改自身修改后的数据
  asynSetUserData(context, payload) {
    request({
      url: '/changeUserData.php',
      params: payload
    }).then(res => {
      context.commit('setUserData', res);
    }).catch(err => {
      console.log(err);
    })
  },
  // 通过用户Id获取用户数据
  asynHasIdGetUser(context, payload) {
    request({
      url: '/hasIdGetUser.php',
      params: payload
    }).then(res => {
      context.commit('getChangeUserData', res);
    }).catch(err => {
      console.log(err);
    })
  },
  // 发送天气请求
  asynGetWeather(context) {
    Vue.prototype.$ajax.all([Vue.prototype.$ajax({
      url: 'https://devapi.qweather.com/v7/weather/now',
      params: {
        key: 'b15e71096d6541cab96ec0f8ed42ac2d',
        location: 101270107
      }
    }), Vue.prototype.$ajax({
      url: 'https://devapi.qweather.com/v7/indices/1d',
      params: {
        key: 'b15e71096d6541cab96ec0f8ed42ac2d',
        location: 101270107,
        type: 13
      }
    }), Vue.prototype.$ajax({
      url: 'https://api.qweather.com/v7/astronomy/sun',
      params: {
        key: 'ed941d2a09c8402db1c4e378b237fa16',
        location: 101270107,
        date: context.state.nowDateUpWeather
      }
    })]).then(Vue.prototype.$ajax.spread((res1, res2, res3) => {
      let data = [res1.data.now, res2.data.daily[0], res3.data] //数据处理
      context.commit('getWeather', data);
    })).catch(err => {
      console.log(err);
    })
  },
  // 请求用户数据渲染到表格
  asynGetUserTable(context, payload) {
    request({
      url: '/getUserTable.php',
      params: {
        searchName: payload
      }
    }).then(res => {
      context.commit('getUserTable', res);
    }).catch(err => {
      console.log(err);
    })
  },
  // 请求书籍数据渲染到表格
  asynGetBooksTable(context, payload) {
    request({
      url: '/getBooksTable.php',
      params: {
        searchBooks: payload
      }
    }).then(res => {
      context.commit('getBooksTable', res);
    }).catch(err => {
      console.log(err);
    })
  },
  // 请求用户评论数据渲染到表格
  asynGetCommentsTable(context, payload) {
    request({
      url: '/getCommentsTable.php',
      params: {
        searchComments: payload
      }
    }).then(res => {
      context.commit('getCommentsTable', res);
    }).catch(err => {
      console.log(err);
    })
  },
  // 删除用户
  asynDeleteUser(context, payload) {
    request({
      url: '/deleteUser.php',
      params: {
        id: payload
      }
    })
  },
  // 删除用户评论
  asynDeleteComments(context, payload) {
    request({
      url: '/deleteComments.php',
      params: {
        id: payload
      }
    }).then(res => {
      console.log(res);
    })
  },
  // 提交修改用户管理员权限
  asynChangeRoot(context, payload) {
    request({
      url: '/changeRoot.php',
      params: payload 
    }).then(res => {
      context.commit('changeRoot', res);
    }).catch(err => {
      console.log(err);
    })
  },
  //异步发送搜索书籍请求
  asynSearchBooks(context, payload) {
    request({
      url: '/searchBooks.php',
      params: {
        booksName: String(payload)
      } 
    }).then(res => {
      context.commit('getSearchBooks', res);
    }).catch(err => {
      console.log(err);
    })
  },
  //异步请求在线阅读书籍
  asynReadBooks(context, payload) {
    request({
      url: '/getReadBooks.php',
      params: {
        readBooksName: payload + '.txt'
      } 
    }).then(res => {
      context.commit('readBooks', res);
    }).catch(err => {
      console.log(err);
    });
  },
  // 上传用户头像
  asynUserImg(context, payload) {
    request({
      url: '/saveUserImg.php',
      params: {
        id: payload.id,
        name: payload.name,
      }
    })
  },
}